/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { useState } from 'react';
import { message } from 'antd';
import type { ProColumns } from '@ant-design/pro-table';
import { getAppCheckList, unfreezeRequest } from '@/services/tools/appLoginCheck';

const commonColumns: ProColumns[] = [
  {
    title: '设备信息',
    dataIndex: 'imei',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '驿站账号',
    dataIndex: 'admin_phone',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '驿站名称',
    dataIndex: 'inn_name',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '所在地区',
    dataIndex: 'concat_location',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '登录账号',
    dataIndex: 'phone',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '本次登录位置',
    dataIndex: 'login_loc',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '登录时间',
    dataIndex: 'create_at',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
];

export const useAppLoginCheck = () => {
  const [dataSource, setDataSource] = useState<any>({});

  const columns: ProColumns[] = [...commonColumns];

  const getTableList = (values: any) => {
    console.log('getTableList.values', values);
    message.loading({
      key: 'loadingKey',
      content: '查询中...',
    });
    getAppCheckList({ page: 1, pageSize: 20, ...(values ? values : {}) }).then((res: any) => {
      message.destroy('loadingKey');
      if (res?.data?.list?.length > 0) {
        setDataSource(res?.data?.list[0]);
      } else {
        setDataSource({});
      }
      if (res.code != 0) {
        message.error(res.msg);
      }
    });
  };

  const onSearch = (values: any) => {
    console.log('onSearch.values', values);
    getTableList(values);
  };

  const onReset = () => {
    setDataSource({});
  };

  const onUnfreeze = () => {
    unfreezeRequest({
      phone: dataSource.phone,
      imei: dataSource.imei,
    }).then((res) => {
      if (res.code == 0) {
        message.success('操作成功');
      } else {
        message.error(res.msg);
      }
    });
  };

  return {
    columns,
    dataSource,
    getTableList,
    onSearch,
    onReset,
    onUnfreeze,
  };
};

// 该驿站最近登录列表
export const YzLoginColumns: ProColumns[] = [
  {
    title: '登录时间',
    dataIndex: 'time',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '登录位置',
    dataIndex: 'area',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
  {
    title: '距离本次登录',
    dataIndex: 'range',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
    render: (_, record) => {
      return `${record.range || '-'}米`;
    },
  },
  {
    title: '设备信息',
    dataIndex: 'imei',
    search: false,
    align: 'center',
    width: 100,
    ellipsis: true,
  },
];

// 该设备最近登录
export const deviceColumns: ProColumns[] = [
  {
    title: '登录时间',
    dataIndex: 'time',
    search: false,
    align: 'center',
    width: 180,
    ellipsis: true,
  },
  {
    title: '登录驿站',
    dataIndex: 'username',
    search: false,
    align: 'center',
    width: 200,
    ellipsis: true,
  },
  {
    title: '登录位置',
    dataIndex: 'login_area',
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '驿站所在地区',
    dataIndex: 'area',
    search: false,
    align: 'center',
    ellipsis: true,
  },
  {
    title: '登录位置距驿站距离',
    dataIndex: 'login_range',
    search: false,
    align: 'center',
    width: 200,
    ellipsis: true,
    render: (_, record) => {
      return `${record.range || '-'}米`;
    },
  },
];
